package com.huage.courseplatform.application.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.huage.courseplatform.application.model.dto.SysFileDTO;
import com.huage.courseplatform.application.model.entity.SysFile;
import com.huage.courseplatform.application.service.FileService;
import com.huage.courseplatform.framework.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;

@Controller
@RequestMapping(value = "/upload")
@Api(tags = "文件模块接口")
@Slf4j
@SaIgnore
public class FileController {

    @Resource
    private FileService fileService;


    @SaCheckLogin
    @ResponseBody
    @ApiOperation(value = "上传图片")
    @PostMapping(value = "/image")
    public ResultUtils<SysFileDTO> imageUpload(@RequestParam(value = "imgFile",required = true) MultipartFile imgFile) throws IOException {
        SysFile sysFile = fileService.uploadFile(imgFile,"image");
        SysFileDTO sysFileDTO = new SysFileDTO();
        BeanUtils.copyProperties(sysFile,sysFileDTO);
        sysFileDTO.setRealPath(sysFile.getPath() + "/" + sysFile.getFileName() + "." + sysFile.getExt());
        return ResultUtils.success(sysFileDTO);
    }

    @SaCheckLogin
    @ResponseBody
    @ApiOperation(value = "上传视频")
    @PostMapping(value = "/video")
    public ResultUtils<SysFileDTO> videoUpload(@RequestParam(value = "videoFile",required = true) MultipartFile videoFile) throws IOException {
        SysFile sysFile = fileService.uploadFile(videoFile,"video");
        SysFileDTO sysFileDTO = new SysFileDTO();
        BeanUtils.copyProperties(sysFile,sysFileDTO);
        sysFileDTO.setRealPath(sysFile.getPath() + "/" + sysFile.getFileName() + "." + sysFile.getExt());
        return ResultUtils.success(sysFileDTO);
    }

    @SaCheckLogin
    @ResponseBody
    @ApiOperation(value = "删除文件")
    @DeleteMapping(value = "/delete/{fileId}")
    public ResultUtils<String> fileDelete(@PathVariable  String fileId){
        return ResultUtils.success(fileService.deleteFile(fileId));
    }


    @ApiOperation(value = "查看图片")
    @GetMapping(value = "/imgById/{fileId}")
    public String img(@PathVariable  String fileId){
        SysFile sysFile = fileService.getById(fileId);
        if(!ObjectUtils.isEmpty(sysFile)) {
            String file = sysFile.getFileName() + "." + sysFile.getExt();
            String path = "/img/" + file;
            return path;
        }else {
            return null;
        }

    }

    @ApiOperation(value = "查看视频")
    @GetMapping(value = "/videoById/{fileId}")
    public String video(@PathVariable  String fileId){
        SysFile sysFile = fileService.getById(fileId);
        if(!ObjectUtils.isEmpty(sysFile)){
            String file =  sysFile.getFileName()+"."+sysFile.getExt();
            String path = "/video/"+file;
            return path;
        }else {
            return null;
        }

    }

}
